package yxy.game.pm2.command.camptower;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.module.battle.BattleType;
import yxy.game.pm2.module.camptower.CampTowerHandle;
import yxy.game.pm2.module.camptower.CampTowerModule;
import yxy.game.pm2.module.hero.FormatHandle;

import java.util.ArrayList;

/**
 * 阵营塔 挑战请求界面
 *
 * @ClassName CMD_CampTowerChanllenge
 * @Author jingtao-zheng
 * @Date 2022/8/1 18:45
 * @Version 1.0
 **/
@MessageType(proto = Command.CampTowerChanllenge, classtype = CampTowerMessage.CampTowerChanllengeRequest.class)
public class CMD_CampTowerChanllenge extends Command<CampTowerMessage.CampTowerChanllengeRequest> {
    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.CampTowerChanllengeRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        CampTowerMessage.CampTowerChanllengeResponse.Builder response = CampTowerMessage.CampTowerChanllengeResponse.newBuilder();
        //获取阵营塔类型
        int type = request.getType().getNumber();
        //获取玩家要挑战层数
        int floor = request.getFloor();
        //活动是否开启
        boolean finish = CampTowerHandle.isFinish(type);
        if(!finish){
            send(messageSource, response, Result.ActNoOpen);
        }
        //判断是否能打该层
        CampTowerModule module = CampTowerModule.get(playerCode);
        boolean isCheckCanPlay = module.checkCanPlay(floor, type);
        if(!isCheckCanPlay){
            send(messageSource, response, Result.CampTowerNotPlayFloor);// 条件不满足,不能打这层
        }
        BattleType battleType = matchType(type);
        CampTowerHandle handle = new CampTowerHandle(battleType);
        //判断阵容是否符合要求
        FormatHandle formatModule = new FormatHandle();
        Format.FormatType formatType = CampTowerHandle.matchFormatType(type);
        if (formatType == null) {
            send(messageSource, response, Result.ParamError);
            return;
        }
        Format format = formatModule.get(playerCode, formatType);

        ArrayList<String> campList = new ArrayList<>();
        campList.add(format.getFu());
        campList.add(format.getFm());
        campList.add(format.getFd());
        campList.add(format.getBu());
        campList.add(format.getBm());
        campList.add(format.getBd());
        boolean isPass = CampTowerHandle.checkCamp(campList, type, 1, playerCode);
        if (format.getFu() == null || format.getFm() == null || format.getFd() == null || format.getBu() == null || format.getBm() == null || format.getBd() == null) {
            // 必须上阵6名武将
            send(messageSource, response, Result.CampTowerSixGeneralsRequired);
            return;
        }
        if (!isPass) {
            send(messageSource, response, Result.CampTowerDoesNotMeetTheRequirements);
            return;
        }
        //之前战斗是否结束
        if (handle.isBattling(playerCode)) {
            send(messageSource, response, Result.BattleIsRunning);// 之前的战斗未结束
            return;
        }
        FormatHandle formatHandle = new FormatHandle();
        Result result = formatHandle.check(playerCode, formatHandle.get(playerCode, formatType));
        //  重复武将判断
        if (!result.equals(Result.Success)) {
            send(messageSource, response, result);
            return;
        }
        //TODO 判断布阵条件
        //判断挑战次数
        //已经挑战过的层数
        Integer challengedFloor = module.getFloor().get(type);
        if (floor > challengedFloor + 1) {
            send(messageSource, response, Result.TowerChanllengeFloor);// 你还不能挑战这层
            return;
        }
        //判断挑战次数
        Integer challengedTimes = module.getTimes().get(type);
        if (challengedTimes <= 0) {
            send(messageSource, response, Result.TowerChanllengeTimesLack);// 没有挑战次数了
            return;
        }

        try {
            response.setBattle(handle.battleResponse(playerCode, floor, floor > challengedFloor,type));
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, CampTowerMessage.CampTowerChanllengeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

    /**
     * 匹配阵营塔类型
     *
     * @param key
     */
    private BattleType matchType(int key) {
        if (key == Constant.WEI) {
            return BattleType.WEI_TOWER;
        }
        if (key == Constant.SHU) {
            return BattleType.SHU_TOWER;
        }
        if (key == Constant.WU) {
            return BattleType.WU_TOWER;
        }
        if (key == Constant.QUN) {
            return BattleType.QUN_TOWER;
        }
        return null;
    }

    /**
     * 匹配阵营塔阵容类型
     *
     * @param key
     */
    private Format.FormatType matchFormatType(int key) {
        if (key == Constant.WEI) {
            return Format.FormatType.魏国塔;
        }
        if (key == Constant.SHU) {
            return Format.FormatType.蜀国塔;
        }
        if (key == Constant.WU) {
            return Format.FormatType.吴国塔;
        }
        if (key == Constant.QUN) {
            return Format.FormatType.群国塔;
        }
        return null;
    }

}
